package cn.fyg.service.finance.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class DateUtil {
	private static Logger logger = LogManager.getLogger(DateUtil.class.getName()); 
	private static final DateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd"); 

	/**
	 * 获得当月第一天
	 * @return
	 */
	public static Date getFirstDayOfMonth(){
		Calendar  day=Calendar.getInstance();
		day.set(Calendar.DAY_OF_MONTH, 1);
		return day.getTime();
	}
	
	/**
	 * 获取当月最后一天
	 * @return
	 */
	public static Date getLastDayOfMonth(){
		Calendar  day=Calendar.getInstance();
		day.set(Calendar.DAY_OF_MONTH, day.getActualMaximum(Calendar.DAY_OF_MONTH));
		return day.getTime();
	}
	
	/**
	 *获取当前日期的下一天
	 * @param date
	 * @return
	 */
	public static Date getNextDay(Date date){
		Calendar day=Calendar.getInstance();
		day.setTime(date);
		day.add(Calendar.DAY_OF_MONTH, 1);
		return day.getTime();
	}
	
	/**
	 * 获取当前日期的前一天
	 * @param date
	 * @return
	 */
	public static Date getPreviousDay(Date date){
		Calendar day=Calendar.getInstance();
		day.setTime(date);
		day.add(Calendar.DAY_OF_MONTH, -1);
		return day.getTime();
	}

	/**
	 * yyyy_MM_dd格式 字符串格式化日期
	 * @param dateStr
	 * @return
	 */
	public static Date strToDate(String dateStr){
		try {
			return yyyy_MM_dd.parse(dateStr);
		} catch (ParseException e) {
			logger.info(e.getMessage());
		}
		return null;
	}
	
	/**
	 * yyyy_MM_dd格式 日期格式化成字符串
	 * @param date
	 * @return
	 */
	public static String dateToStr(Date date){
		return yyyy_MM_dd.format(date);
	}
	
	
	/**
	 * yyyy_MM_dd格式 日期格式化成ksql字符串,用于拼装sql查询语句
	 * @param date
	 * @return
	 */
	public static String formatSqlDate(Date date) {
    	return "{TS '"+yyyy_MM_dd.format(date)+" 00:00:00'}";
	}
	
	/**
	 * 返回year年，month月，date天，0点整
	 * @param year
	 * @param month
	 * @param date
	 * @return
	 */
	public static Date getDate(int year,int month,int date){
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, date,0,0,0);
		return calendar.getTime();
	}
}
